/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.pz.net.master;

import com.pz.util.UserCreationResult;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;

/**
 *
 * @author jannek
 */
public interface MasterServerListener {

    /**
     * Client attempts to connect as a user. If you keep track of your users,
     * you should only accept users who have already registered them selves
     * with userCreated(). Otherwise you should just accept them, with an
     * available id.
     *
     * If the returned id is in use, the previous holder will be removed from
     * the master server, this will not affect any users currently connected to
     * a server (only if the new holder connects to the same server).
     *
     * Both users and servers share the same id pool.
     *
     * @param name User name.
     * @param data Custom data.. Perhaps a password :)
     * @param address Ip address from wich the user has connected.
     * @return Positive unique client id if connection was accepted, -1 if not.
     */
    public int userConnected(String name, ByteBuffer data,
            InetSocketAddress address);

    /**
     * Client connected as a user logged out.
     *
     * @param user Id of the leaving user.
     */
    public void userLoggingOut(int user);

    /**
     * You should only use this, if you intend to keep track of your users.
     * The purpose of this method is solely to allow you to register new users
     * in your database. It does not alter any thing in the master server. It is
     * more like a utillity method.
     *
     * A good reason to reject a new user, could be an already used user name,
     * or a blocked ip.
     *
     * Remember that the data block is entirely custom. You could put the real
     * user name in it, if you don't want the displayed player name to be
     * unchangeable, that way you can allow the user to pick whatever name it
     * desires whenever it logs in.
     *
     * @param name Desired user name or random string.
     * @param data Password / real desired user name / whatever.
     * @param address Ip address from wich the user has connected.
     * @return
     */
    public UserCreationResult userCreated(String name, ByteBuffer data,
            InetSocketAddress address);

    /**
     *
     *
     * @param serverName
     * @param userName
     * @param data
     * @param address
     * @return
     */
    public int serverConnected(String serverName, String userName,
            ByteBuffer data, InetSocketAddress address);

    /**
     * A server has confirmed it's udp port, and is now ready for connections.
     *
     * @param server The now ready server.
     */
    public void serverActive(int server);

    /**
     * A server has stopped/disconnected. All connected players have been
     * disconnected from the server, and the server does no longer exist in the
     * master servers model.
     *
     * Just because the server has been disconnected, it doesn't means that it
     * has stopped. Games like most strategy games without hot join should not
     * be on the server list, if they does not accept any players.
     *
     * @param server The now remoeved server.
     */
    public void serverStopped(int server);

    /**
     * A custom message from a user. This is intended as a way for an admin to
     * communicate with a running server.
     *
     * @param user The user who sends the message.
     * @param data The message.
     */
    public void customMessage(int user, ByteBuffer data);

    /**
     * Determines whether or not the user is allowed to see the server list.
     *
     * @param user Who asks.
     * @return Allow the user to see the list.
     */
    public boolean userRequestsServerList(int user);

    /**
     * Determines whether or not the user is allowed to see detailed server
     * informations for a specific server.
     *
     * @param user Who asks.
     * @param server The requested server.
     * @return Allow the user to see the info.
     */
    public boolean userRequestsServerInfo(int user, int server);

    /**
     * Determines whether or not the user is allowed to connect to the given
     * server.
     *
     * @param user The user.
     * @param server The server.
     * @return Allow the user to connect.
     */
    public boolean userConnectsToServer(int user, int server);

    public void userSuccessfullyConnectedToServer(int user, int server);

    public void userFailedToConnectToServer(int user, int server);

    public void userLeftServer(int user, int server);
}
